﻿; !function () {
    var layout = {
        v: "2.2"
    };
    var obj = function (selector) {
        this.selector = $(selector);
    };
    obj.prototype.layout = function () {
        var _obj = this;
        var containerWidth = this.selector.width();
        var containerHeight = this.selector.height();
        _obj.selector.addClass('kui-layout');
        //if (_obj.selector.css('position') != 'fixed' && _obj.selector.css('position') != 'relative') {
        //    _obj.selector.css('position', 'relative');
        //}
        if (!_obj.selector.css('position')) {
            _obj.selector.css('position', 'absolute');
        }
        $.each(this.selector.children(), function (i, o) {
            $(o).css('position','absolute');
        })
        $.each(this.selector.children(), function (i, o) {
            //if (!$(o).hasClass('kui-layout')) {
            //    return;
            //}
            var leftAttr = $(o).attr('left');
            var centerAttr = $(o).attr('center');
            var rightAttr = $(o).attr('right');
            var topAttr = $(o).attr('top');
            var middleAttr = $(o).attr('middle');
            var bottomAttr = $(o).attr('bottom');

            //left、top、right、bottom微调值.
            var leftAdd = 0;
            var topAdd = 0;
            var rightAdd = 0;
            var bottomAdd = 0;
            var centerAdd = 0;
            var middleAdd = 0;
            var leftAddAttr = $(o).attr('leftAdd');
            var topAddAttr = $(o).attr('topAdd');
            var rightAddAttr = $(o).attr('rightAdd');
            var bottomAddAttr = $(o).attr('bottomAdd');
            var centerAddAttr = $(o).attr('centerAdd');
            var middleAddAttr = $(o).attr('middleAdd');

            if (leftAddAttr) {
                leftAdd = parseInt(leftAddAttr, 10);
            }
            if (topAddAttr) {
                topAdd = parseInt(topAddAttr, 10);
            }
            if (rightAddAttr) {
                rightAdd = parseInt(rightAddAttr, 10);
            }
            if (bottomAddAttr) {
                bottomAdd = parseInt(bottomAddAttr, 10);
            }
            if (centerAddAttr) {
                centerAdd = parseInt(centerAddAttr, 10);
            }
            if (middleAddAttr) {
                middleAdd = parseInt(middleAddAttr, 10);
            }
            //横向定义4种情况:仅定义left、仅定义center、仅定义right、同时定义left和right.
            //纵向定义与横向定义类似.
            
            var left = 0;
            var top = 0;
            var width = $(o).width();
            var height = $(o).height();

            //left
            if (leftAttr == ':left') {
                left = 0;
            }
            else if (leftAttr == ':center') {
                left = containerWidth / 2;
            }
            else if(leftAttr==':right')
            {
                left = containerWidth;
            }
            else if (leftAttr&&leftAttr.indexOf(':')>0) {
                var arr = leftAttr.split(':');
                var arr0 = arr[0];
                var arr1 = arr[1];
                if (arr1 == 'left') {
                    left = $(arr0).position().left;
                }
                else if (arr1 == 'center') {
                    left = $(arr0).position().left + $(arr0).width()/2;
                }
                else if (arr1 == 'right') {
                    left = $(arr0).position().left + $(arr0).width();
                }
            }
            left += leftAdd;

            //right
            if (!leftAttr) {
                if (rightAttr == ':left') {
                    left = 0 - $(o).width();
                }
                else if (rightAttr == ':center') {
                    left = containerWidth / 2 - $(o).width();
                }
                else if (rightAttr == ':right') {
                    left = containerWidth - $(o).width();
                }
                else if (rightAttr && rightAttr.indexOf(':') > 0) {
                    var arr = rightAttr.split(':');
                    var arr0 = arr[0];
                    var arr1 = arr[1];
                    if (arr1 == 'left') {
                        left = $(arr0).position().left-$(o).width();
                    }
                    else if (arr1 == 'center') {
                        left = $(arr0).position().left + $(arr0).width() / 2 - $(o).width();
                    }
                    else if (arr1 == 'right') {
                        left = $(arr0).position().left + $(arr0).width() - $(o).width();
                    }
                }
                left += rightAdd;
            }
            else
            {
                if (rightAttr == ':left') {
                    width = 0 - left;
                }
                else if (rightAttr == ':center') {
                    width = containerWidth / 2 - left;
                }
                else if (rightAttr == ':right') {
                    width = containerWidth - left;
                }
                else if (rightAttr && rightAttr.indexOf(':') > 0) {
                    var arr = rightAttr.split(':');
                    var arr0 = arr[0];
                    var arr1 = arr[1];
                    if (arr1 == 'left') {
                        width = $(arr0).position().left - left;
                    }
                    else if (arr1 == 'center') {
                        width = $(arr0).position().left + $(arr0).width() / 2 - left;
                    }
                    else if (arr1 == 'right') {
                        width = $(arr0).position().left + $(arr0).width() - left;
                    }
                }
                width += rightAdd;
            }

            //top
            if (topAttr == ':top') {
                top = 0;
            }
            else if (topAttr == ':middle') {
                top = containerHeight / 2;
            }
            else if (topAttr == ':bottom') {
                top = containerHeight;
            }
            else if (topAttr && topAttr.indexOf(':') > 0) {
                var arr = topAttr.split(':');
                var arr0 = arr[0];
                var arr1 = arr[1];
                if (arr1 == 'top') {
                    top = $(arr0).position().top;
                }
                else if (arr1 == 'middle') {
                    top = $(arr0).position().top + $(arr0).height() / 2;
                }
                else if (arr1 == 'bottom') {
                    top = $(arr0).position().top + $(arr0).height();
                }
            }
            top += topAdd;

            //bottom
            if (!topAttr) {
                if (bottomAttr == ':top') {
                    top = 0 - $(o).height();
                }
                else if (bottomAttr == ':middle') {
                    top = containerHeight / 2 - $(o).height();
                }
                else if (bottomAttr == ':bottom') {
                    top = containerHeight - $(o).height();
                }
                else if (bottomAttr && bottomAttr.indexOf(':') > 0) {
                    var arr = bottomAttr.split(':');
                    var arr0 = arr[0];
                    var arr1 = arr[1];
                    if (arr1 == 'top') {
                        top = $(arr0).position().top - $(o).height();
                    }
                    else if (arr1 == 'middle') {
                        top = $(arr0).position().top + $(arr0).height() / 2 - $(o).height();
                    }
                    else if (arr1 == 'bottom') {
                        top = $(arr0).position().top + $(arr0).height() - $(o).height();
                    }
                }
                top += bottomAdd;
            }
            else {
                if (bottomAttr == ':top') {
                    height = 0 - top;
                }
                else if (bottomAttr == ':middle') {
                    height = containerHeight / 2 - top;
                }
                else if (bottomAttr == ':bottom') {
                    height = containerHeight - top;
                }
                else if (bottomAttr && bottomAttr.indexOf(':') > 0) {
                    var arr = bottomAttr.split(':');
                    var arr0 = arr[0];
                    var arr1 = arr[1];
                    if (arr1 == 'top') {
                        height = $(arr0).position().top - top;
                    }
                    else if (arr1 == 'middle') {
                        height = $(arr0).position().top + $(arr0).height() / 2 - top;
                    }
                    else if (arr1 == 'bottom') {
                        height = $(arr0).position().top + $(arr0).height() - top;
                    }
                }
                height += bottomAdd;
            }

            //center
            if (centerAttr == ':left') {
                left =0-$(o).width() / 2;
            }
            else if (centerAttr == ':center') {
                left = (containerWidth - $(o).width()) / 2;
            }
            else if (centerAttr == ':right') {
                left = containerWidth - $(o).width()/2;
            }
            else if (centerAttr && centerAttr.indexOf(':') > 0) {
                var arr = centerAttr.split(':');
                var arr0 = arr[0];
                var arr1 = arr[1];
                if (arr1 == 'left') {
                    left = $(arr0).position().left - $(o).width() / 2;
                }
                else if (arr1 == 'center') {
                    left = $(arr0).position().left + $(arr0).width() / 2 - $(o).width() / 2;
                }
                else if (arr1 == 'right') {
                    left = $(arr0).position().left + $(arr0).width() - $(o).width() / 2;
                }
            }
            left += centerAdd;

            //middle
            if (middleAttr == ':top') {
                top = 0 - $(o).height() / 2;
            }
            else if (middleAttr == ':middle') {
                top = (containerHeight - $(o).height()) / 2;
            }
            else if (middleAttr == ':bottom') {
                top = containerHeight - $(o).height() / 2;
            }
            else if (middleAttr && middleAttr.indexOf(':') > 0) {
                var arr = middleAttr.split(':');
                var arr0 = arr[0];
                var arr1 = arr[1];
                if (arr1 == 'top') {
                    top = $(arr0).position().top - $(o).height() / 2;
                }
                else if (arr1 == 'middle') {
                    top = $(arr0).position().top + $(arr0).height() / 2 - $(o).height() / 2;
                }
                else if (arr1 == 'bottom') {
                    top = $(arr0).position().top + $(arr0).height() - $(o).height() / 2;
                }
            }
            top += middleAdd;
            

            if (leftAttr || rightAttr || centerAttr) {
                $(o).css('left', left);
                $(o).css('width', width);
            }
            if (topAttr || middleAttr || bottomAttr) {
                $(o).css('top', top);
                $(o).css('height', height);
                //$(o).css('line-height', height);
            }
        })

        //自动调用split
        $.each(_obj.selector.children('.kui-split'), function (i, o) {
            if (o.isSplit == true) {
                return;//split永远只调一次.
            }
            o.isSplit = true;
            var direction = $(o).attr('split-direction');
            $(o).split(direction);
        });

        //layout嵌套.
        $.each(_obj.selector.find('.kui-layout'), function (i,o) {
            new obj(o).layout();
            
        })
    }
   
    layout.layout = function (selector) {
        var _obj = new obj(selector);
        $(window).resize(function () {
            _obj.layout();
        });
        _obj.layout();
    };
   
    window.layout = layout;

    //实现自动调用.
    $(function () {
        $.each($('body').find('.kui-layout'), function (i, o) {
            window.layout.layout(o);
        });
    })
    
}();